package com.haiqi.modules.ship.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.*;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.ship.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/5/27.
 */
@Controller
@RequestMapping("/manage/ship/user")
@SafeAuthority(menuCode = "accountMobile,monitorLocation")
public class ShipUserController extends BaseController {

    @Autowired
    private ShipUserService shipUserService;

    @Autowired
    private AisShipStaticService aisShipStaticService;

    @Autowired
    private FleetService fleetService;
    @Autowired
    private AisShipDynaService aisShipDynaService;

    @Autowired
    private ShipFollowService shipFollowService;

    @Autowired
    private  ShipService shipService;

    @Autowired
    private GuanZhuService guanZhuService;

    @Autowired
    private  AddShipService addShipService;

    private String menuCode = "accountMobile";

    @RequestMapping("/list")
    public String list(CrewUser crewUser, PageBean pageBean, HttpSession session, Model model){
        generateMenuCode(session, menuCode);
        ResponseBean.UserBean user = getUser(session);
        crewUser.setCompanyId(user.getOfficeid());
        PageInfo<CrewUser> pageInfo = shipUserService.getDataList(crewUser,pageBean);
        model.addAttribute("param",crewUser);
        model.addAttribute("pageInfo",pageInfo);
        return "/modules/ship/user/list";
    }

    @RequestMapping("/ajax/list")
    @ResponseBody
    public List<CrewUser> getJsonList(CrewUser crewUser, HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        crewUser.setCompanyId(user.getOfficeid());
        List<CrewUser> shipInfoList = shipUserService.getDataList(crewUser);
        return shipInfoList;
    }
    @RequestMapping("/fleet/list")
    @ResponseBody
    public DataGridBean<CrewUser> getAjaxList(String shipName,String searchType,PageBean pageBean, HttpSession session){
        PageUtils.generatePage(pageBean);
        ResponseBean.UserBean user = getUser(session);
        FleetInfo fleetInfo = null;
        List<CrewUser> list = null;
        if(!StringUtils.isEmpty(searchType) || searchType != null){
            if(searchType.equals("1")){ //查询本公司船队的船舶信息
                list = shipUserService.getListByCompanyId(user.getOfficeid(),"1",shipName);
            }else if(searchType.equals("2")){  //查询所有公司船队的船舶信息
                if(!StringUtils.isEmpty(shipName)){
                    list = shipUserService.getListByCompanyId(null,"1",shipName);
                    if(list != null &&list.size()>0){
                        for(int i=0;i<list.size();i++){
                            if(!list.get(i).getCompanyId().equals(user.getOfficeid())){
                                //判断查询到的船舶是否含有九位码，有的话，则插入，没有的话，则跳过
                                if(!StringUtils.isEmpty(list.get(i).getMmsi())){
                                    //判断该船舶九位码与船公司ID是否已经存在与数据库中，已存在的话，跳过
                                    fleetInfo = fleetService.getOneFleetById(user.getOfficeid(),list.get(i).getMmsi());
                                    if(fleetInfo ==null){
                                        fleetInfo = new FleetInfo();
                                        fleetInfo.setCompanyId(user.getOfficeid());
                                        fleetInfo.setMmsi(list.get(i).getMmsi());
                                        fleetService.insertOneFleet(fleetInfo);
                                    }
                                }
                            }
                        }
                    }
                }else{
                    list = shipUserService.getListByCompanyId(null,"1",null);
                }
            }
        }else{
            list = shipUserService.getListByCompanyId(user.getOfficeid(),"1",shipName);
        }

        PageInfo<CrewUser> pageInfo = new PageInfo<>(list);
        DataGridBean<CrewUser> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }

    @RequestMapping("/fleet/other/list")
    @ResponseBody
    public DataGridBean<AisShipStatic> getOtherShipList(String shipName, PageBean pageBean, HttpSession session){
        PageUtils.generatePage(pageBean);
        ResponseBean.UserBean user = getUser(session);
        List<AisShipStatic> list = aisShipStaticService.getAllListByShipNameOrMmsi(shipName);
        PageInfo<AisShipStatic> pageInfo = new PageInfo<>(list);
        DataGridBean<AisShipStatic> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public CrewUser getShipInfo(CrewUser crewUser){
        CrewUser crewUserInfo = shipUserService.getCrewUserInfo(crewUser.getId());
        return crewUserInfo;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map updateShipInfo(CrewUser crewUser, HttpSession session){
        Map response = new HashMap<>();
        ResponseBean.UserBean user = getUser(session);
        crewUser.setCompanyId(user.getOfficeid());
        if("1".equals(crewUser.getUserType()))
        {
            ShipInfo shipInfo=shipService.getShipInfo(crewUser.getShipid());
            crewUser.setShipName(shipInfo.getShipName());
        }
        if("2".equals(crewUser.getUserType())){ //监控用户不可以有船舶编号
            crewUser.setShipid(null);
        }
        if(crewUser.getId() == null){
            CrewUser selectByUserName = shipUserService.selectByUserName(crewUser.getLoginName());
            if(selectByUserName != null){
                response.put("success",false);
                response.put("response","用户已存在");
                return response;
            }else if(!"2".equals(crewUser.getUserType())){
                CrewUser crewUser1 = new CrewUser();
                crewUser1.setShipid(crewUser.getShipid());
                List<CrewUser> dataList = shipUserService.getDataList(crewUser1);
                if(dataList != null && dataList.size() >= 1){
                    response.put("success",false);
                    response.put("response","该船舶已有账号");
                    return response;
                }
            }
        }
//
//        if(PhoneFormatCheckUtils.isPhoneLegal(crewUser.getTel())==false)
//        {
//            response.put("success",false);
//            response.put("response","手机号码格式错误");
//            return response;
//        }


        boolean success = shipUserService.update(crewUser);
        response.put("success",success);
        return response;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(String id){
        String[] ids = id.split(",");
        boolean success = false;
        Map<String,Boolean> response = new HashMap<>();
        for (String s : ids) {
            success = shipUserService.delete(Integer.valueOf(s));
        }
        response.put("success",success);
        return response;
    }
    @ResponseBody
    @RequestMapping("/addship")
    public Map addship (String mmsi,String id,String name,HttpSession session) {
        ResponseBean.UserBean user = getUser(session);
        Map response = new HashMap<>();
        ShipFollow shipFollow1 = shipFollowService.findBymmsiAndGuanid(mmsi,Integer.parseInt(id));
        if (shipFollow1 != null) {
            response.put("result", false);
            return response;
        } else {
            ShipFollow shipFollow = new ShipFollow();
                shipFollow.setMmsi(mmsi);
                shipFollow.setShipname(name);
                shipFollow.setGuanid(Integer.parseInt(id));
            boolean result = shipFollowService.addship(shipFollow);
            response.put("result", result);
            return response;
        }
    }

    @ResponseBody
    @RequestMapping("/changeGroup")
    public Map changeGroup (ShipFollow shipFollow) {
        Map response = new HashMap<>();
        ShipFollow shipFollow1 = shipFollowService.findBymmsiAndGuanid(shipFollow.getMmsi(),shipFollow.getGuanid());
        if (shipFollow1 != null) {
            response.put("result", false);
            return response;
        } else {
            boolean result = shipFollowService.updateFollow(shipFollow);
            response.put("result", result);
            return response;
        }
    }

    @ResponseBody
    @RequestMapping("/findByMmsi")
    public List<AisShipDyna> findByMmsi(String mmsi ){
        String[] str = mmsi.split(",");
        List<AisShipDyna> list = aisShipDynaService.getlocation(str);
        return list;
        //return aisShipDynaService.selectLocation(mmsi);
    }
    @ResponseBody
    @RequestMapping("/selectLocation")
    public AisShipDyna selectLocation(String mmsi){
        return aisShipDynaService.selectLocation(mmsi);
    }
}
